package com.open.capacity.reimburse.service.impl;


import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.dao.HtAgreementAuthorizeDao;
import com.open.capacity.reimburse.dao.HtAgreementAuthorizeDao;
import com.open.capacity.reimburse.entity.HtAgreementAuthorize;
import com.open.capacity.reimburse.service.HtAgreementAuthorizeService;
import com.open.capacity.reimburse.service.HtAgreementAuthorizeService;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Service
public class HtAgreementAuthorizeServiceImpl implements HtAgreementAuthorizeService {

    @Autowired
    private HtAgreementAuthorizeDao agreementAuthorizeDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(HtAgreementAuthorize authorize) {
        if (StringUtils.isEmpty(authorize.getAuthType())) {
            authorize.setAuthType("1");//默认添加查看权限
        }
        agreementAuthorizeDao.save(authorize);
        log.info("保存合同共享授权：{}", authorize);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(HtAgreementAuthorize confluence) {

        agreementAuthorizeDao.updateByOps(confluence);
        log.info("修改合同共享授权：{}", confluence);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        HtAgreementAuthorize confluence = agreementAuthorizeDao.findById(id);

        agreementAuthorizeDao.delete(id);

        log.info("删除合同共享授权：{}", confluence);
    }


    @Override
    public HtAgreementAuthorize findById(String id) {
        return agreementAuthorizeDao.findById(id);
    }

    @Override
    public List<HtAgreementAuthorize> findByAgreementId(String fileinfoId) {
        return agreementAuthorizeDao.findByAgreementId(fileinfoId);
    }


    @Override
    public Result saveOrUpdate(HtAgreementAuthorize agreementAuthorize, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            HtAgreementAuthorize model = agreementAuthorizeDao.findById(keyValue);
            if (model != null) {
                agreementAuthorize.setId(keyValue);
                i = agreementAuthorizeDao.updateByOps(agreementAuthorize);
            }
        } else {
            agreementAuthorize.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            i = agreementAuthorizeDao.save(agreementAuthorize);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
